/**
 * 封装 axios 请求模块
 */
import axios from 'axios'
import store from '@/store'

const request = axios.create({
  baseURL: 'http://192.168.14.75:9090/api' // 基础路径
})

// 请求拦截器
// Add a request interceptor
request.interceptors.request.use(function (config) {
  // config ：本次请求的配置对象
  // config 里面有一个属性：headers
  console.log(store.state)
  const { user } = store.state
  if (user && user.token) {
    config.headers.Authorization = `Bearer ${user.token}`
  }
  // config.headers.Authorization = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyZXN1bHQiOnsidXNlcl9pZCI6MzgsInVzZXJfbmFtZSI6ImFkbWluNyIsInBhc3N3b3JkIjoiIiwibmFtZSI6bnVsbCwicGhvbmUiOm51bGwsInBvc2l0aW9uIjpudWxsLCJzdGF0ZSI6InZhbGlkIn0sImlhdCI6MTYwMjQ4NjI5OSwiZXhwIjoxNjAyNTcyNjk5fQ.yL7hx9G6pQRbdJPz7X6531EtXiEVmT3Zbs8su6Ryhcc'
  return config
}, function (error) {
  // Do something with request error
  // 如果请求出错了,(还没有发出去) 会进入这里
  return Promise.reject(error)
})

export default request
